package inl.resilienceManager.qmul.gemom;

import java.io.File;
import java.util.Vector;

import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

import resilienceManager.qmul.gemom.Gbroker;

/* 
* COPYRIGHT DISCLAIMER
* Synopsis: All the source codes presented in this document are intellectual
* properties of Queen Mary, University of London.
* 
* @year 2010
* @author Peng Jiang, John Bigham, Jiayi Wu Jinfu Wang------------QMUL
* @version 1.0 
*/

public class gTable {

	JTable table;
	DefaultTableModel model;

	Vector<Gbroker> gvector;

	String[] columnNames = {"ID","Capability","Threshold","IP"};

	public gTable(){

		model= new DefaultTableModel(columnNames,0);

		table = new JTable(model);

		table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);

		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		gvector= new Vector<Gbroker>();

	}

	public JTable getTable(){
		return table;
	}

	public Vector<Gbroker> getGlist(){

		return gvector;
	}

	public void createRow(double cap, double th)
	{
		Object[] newRow = new Object[3];

		int id=1;
		if(model.getRowCount()>0)
			id=gvector.get(gvector.size()-1).getID()+1; 

		newRow[0] = id;
		newRow[1] = cap;
		newRow[2] = th;
//		newRow[3] =ip;

		Gbroker gt=new Gbroker(id,cap,th);

		gvector.add(gt);

		model.addRow(newRow);
	}
	public void createRow(Gbroker b, double th)
	{
		Object[] newRow = new Object[4];
		
		int id=1;
		if(model.getRowCount()>0)
			id=gvector.get(gvector.size()-1).getID()+1; 
		
		newRow[0] = b.getID();
		newRow[1] = b.getCAP();
		newRow[2] = th;
		newRow[3] =b.getIP();
		
		Gbroker gt=new Gbroker(b.getID(),b.getCAP(),th,b.getIP());
		
		gvector.add(gt);
		
		model.addRow(newRow);
	}

	public void deletRow(int rowID)
	{
		if(rowID< model.getRowCount())
		{

			int gid= Integer.parseInt(model.getValueAt(rowID, 0).toString());
			
			//System.out.println("ID="+gid);

			for(int i=0; i< gvector.size(); i++){

				Gbroker t= gvector.get(i);

				if(t.getID()==gid)
					gvector.remove(i);
			}
			model.removeRow(rowID);
		}



	}

	public void modifyRow(int id, double pc,double pth)
	{
		int gid= Integer.parseInt(model.getValueAt(id, 0).toString());


		model.removeRow(id);



		for(int i=0; i< gvector.size(); i++){

			Gbroker t= gvector.get(i);

			if(t.getID()==id)
				gvector.remove(i);
		}

		Object[] newRow = new Object[3];
		newRow[0] = gid ;
		newRow[1] = pc;
		newRow[2] = pth;

		model.insertRow(id, newRow);


	}


}
